********Create lifetime experienced real GDP growth mean and real GDP growth standard deviation********

*Get birth province for individuals, in MXFLS1-MXFLS3
cd "$mxfls1rawdata/hh02dta_b3a"
use iiia_portad.dta, clear

merge 1:1 folio ls using iiia_mg.dta, keep(match) nogen

gen birthstate_mx1=.
replace birthstate_mx1=mg01e_2 if mg01e_1==1
replace birthstate_mx1=ent if mg01e_1==3

gen yearinterviewmx1=2002
keep folio ls birthstate yearinterview
tostring folio ls, replace
replace ls="0"+ls if length(ls)==1
gen pid_link=folio+ls
destring folio ls pid_link, replace

tempfile indivbirprovmex
save "`indivbirprovmex'", replace

cd "$mxfls2rawdata/hh05dta_b3a"
use iiia_portad.dta, clear

merge 1:1 pid_link using iiia_mg.dta, nogen keep(match)
gen birthstate_mx2=.
*Birth state for newly-added individuals
replace birthstate_mx2=mg01e_2 if mg01e_1==1
replace birthstate_mx2=ent if mg01e_1==3

preserve
use iiia_conpor, clear
egen tok=max(min), by(pid_link)
keep if tok==min
duplicates drop pid_link, force
tempfile conportemp
save "`conportemp'", replace
restore

merge 1:1 pid_link using "`conportemp'", nogen keep(match)
gen yearinterviewmx2=anio

keep folio pid_link birthstate* yearinterview*
destring folio pid_link, replace
merge 1:1 pid_link using "`indivbirprovmex'", gen(_mergebirprov2)
save "`indivbirprovmex'", replace

cd "$mxfls3rawdata/hh09dta_b3a"
use iiia_portad.dta, clear
merge 1:1 pid_link using iiia_mg.dta, nogen keep(match)
gen birthstate_mx3=.
*Birth state for newly-added individuals
replace birthstate_mx3=mg01e_2 if mg01e_1==1
replace birthstate_mx3=ent if mg01e_1==3

preserve
use iiia_conpor, clear
egen tok=max(min), by(pid_link)
keep if tok==min
duplicates drop pid_link, force
tempfile conportemp3
save "`conportemp3'", replace
restore

merge 1:1 pid_link using "`conportemp3'", nogen keep(match)
gen yearinterviewmx3=anio

keep folio pid_link birthstate yearinterview*

local lets P H 
local i=0
foreach let in `lets' {
local i=`i'+1
if `i'==1{
gen posi=strpos(folio,"`let'")
gen folio1=substr(folio,1,posi-2)
gen folio2=substr(folio,posi+1,length(folio))
gen folioint=folio1+folio2

gen posi1=strpos(pid_link,"`let'")
gen pidi1=substr(pid_link,1,posi1-2)
gen pidi2=substr(pid_link,posi1+1,length(pid_link))
gen pidiint=pidi1+pidi2
}
if `i'==2{
gen posi2=strpos(folio,"`let'")
gen folio12=substr(folio,1,posi2-2)
gen folio22=substr(folio,posi2+1,length(folio))
gen folioint2=folio12+folio22

gen posi12=strpos(pid_link,"`let'")
gen pidi12=substr(pid_link,1,posi12-2)
gen pidi22=substr(pid_link,posi12+1,length(pid_link))
gen pidiint2=pidi12+pidi22
}
}

rename folio folioold
gen folio=folioint if posi2==0
replace folio=folioint2 if posi2!=0

rename pid_link pidlinkold
gen pid_link=pidiint if posi12==0
replace pid_link=pidiint2 if posi12!=0

keep folio pid_link birthstate yearinterview*
destring folio pid_link, replace

merge 1:1 pid_link using "`indivbirprovmex'", gen(_mergebirprov3)
keep folio pid_link birthstate* yearinterview* _mergebirprov?
save "`indivbirprovmex'", replace


*Attach birth year, in MXFLS2-MXFLS3

cd "$mxfls2rawdata/hh05dta_bC"
use c_ls.dta, clear

gen birthyr_mx2=ls03_22

keep folio panel pid_link birthyr_mx2
drop if pid_link==""
destring pid_link folio, replace

keep folio pid_link birthyr*

merge 1:1 pid_link using "`indivbirprovmex'", gen(_mergebiryear2)
save "`indivbirprovmex'", replace


cd "$mxfls3rawdata/hh09dta_bC"
use c_ls.dta, clear

gen birthyr_mx3=ls03_22

keep folio panel pid_link birthyr_mx3
local lets P H 
local i=0
foreach let in `lets' {
local i=`i'+1
if `i'==1{
gen posi=strpos(folio,"`let'")
gen folio1=substr(folio,1,posi-2)
gen folio2=substr(folio,posi+1,length(folio))
gen folioint=folio1+folio2

gen posi1=strpos(pid_link,"`let'")
gen pidi1=substr(pid_link,1,posi1-2)
gen pidi2=substr(pid_link,posi1+1,length(pid_link))
gen pidiint=pidi1+pidi2
}
if `i'==2{
gen posi2=strpos(folio,"`let'")
gen folio12=substr(folio,1,posi2-2)
gen folio22=substr(folio,posi2+1,length(folio))
gen folioint2=folio12+folio22

gen posi12=strpos(pid_link,"`let'")
gen pidi12=substr(pid_link,1,posi12-2)
gen pidi22=substr(pid_link,posi12+1,length(pid_link))
gen pidiint2=pidi12+pidi22
}
}

rename folio folioold
gen folio=folioint if posi2==0
replace folio=folioint2 if posi2!=0

rename pid_link pidlinkold
gen pid_link=pidiint if posi12==0
replace pid_link=pidiint2 if posi12!=0

duplicates drop pid_link, force
destring folio pid_link, replace
keep folio pid_link birthyr*

cd "$outputdata"
merge 1:1 pid_link using "`indivbirprovmex'", gen(_mergebiryear3)


****Keep only people I observe in 2 and/or 3
keep if _mergebiryear3==3 | _mergebiryear2==3
keep if (_mergebirprov2==1 | _mergebirprov2==3) & (_mergebirprov3==1 | _mergebirprov3==3)

save "`indivbirprovmex'", replace

*Clean GDP growth data

cd "$reploc/data"
use Mex_prov_timeseries.dta, clear

*Winsorize province growth data at 5-95 level across all provinces
reshape long gdpgr, i(provcode) j(year)
winsor gdpgr, p(0.05) gen(gdpgr_wins)
drop gdpgr
rename gdpgr_wins gdpgr
reshape wide gdpgr, i(provcode) j(year)


tempfile prov_timeseries_winsmex
save "`prov_timeseries_winsmex'", replace


***Merge with location data
use "`indivbirprovmex'", clear

gen birthstate=.
replace birthstate=birthstate_mx1 if birthstate_mx1!=.
replace birthstate=birthstate_mx2 if birthstate_mx1==. & birthstate_mx2!=.
replace birthstate=birthstate_mx3 if birthstate_mx1==. & birthstate_mx2==. & birthstate_mx3!=.

gen birthyr=.
replace birthyr=birthyr_mx2 if birthyr_mx2!=.
replace birthyr=birthyr_mx3 if birthyr_mx2==. & birthyr_mx3!=.

gen provcode=birthstate

merge m:1 provcode using "`prov_timeseries_winsmex'", gen(_mergeprovtimeseries)

drop provcode
keep folio pid_link gdpgr* birthstate birthyr yearinterview*


***Generate experienced mean and volatility of growth
foreach var of varlist yearinterviewmx2 yearinterviewmx3 {
replace `var'=`var'+2000
}

gen imp_agemx2 = yearinterviewmx2 - birthyr
gen imp_agemx3 = yearinterviewmx3 - birthyr

foreach var of varlist gdpgr*{
local yr=substr("`var'",6,.)
rename `var' grbr`yr'
}

*Merge in national timesieres
gen idmex=1

***This imports gdp in each year since 1925 for each person 
merge m:1 idmex using Mex_timeseries.dta, gen(_mergemex)
drop idmex
drop _mergemex


local t grbr

quietly {

order grbr*, sequential
local nums mx2 mx3

*Variation 1: Assume missing for individuals born before 1941

	local lam1=0
	local lam2=0
	local numi=5
	
	
	forval lambda1=`lam1'(10)`lam2' {
	
	
	capture drop grbr????_wt1mx?_lam* sumweights* grbr????lam*_mx? grbr????lam*sd_* intSD_lam*_41*
	
		local lambda=round(`lambda1'/100,0.1)

	

		if `lambda'<0 {
			local hola=substr("`lambda1'",2,.)
			local lamnam neg`hola'
		}
		else {
			local lamnam `lambda1'
		}


		*Avg
		
		foreach wav in `nums' {
			foreach var of varlist `t'1941-`t'2014  {

				local curryear=substr("`var'",`numi',.)
				
				
				local k=yearinterview`wav'-`curryear'
				gen `var'_wt1`wav'_lam`lamnam'=.
			
				replace `var'_wt1`wav'_lam`lamnam'=((imp_age`wav'-`k')/imp_age`wav')^`lambda' if `k'<=imp_age`wav' & `curryear'<=yearinterview`wav' & birthyr>=1941 & birthyr<=`curryear'
         
			
			   replace `var'_wt1`wav'_lam`lamnam'=((imp_age`wav'-`k')/imp_age`wav')^`lambda' if birthyr<=1976 & `curryear'<=yearinterview`wav'
			   
				
			}
		


			egen sumweights`wav'_lam`lamnam'=rowtotal(*_wt1`wav'_lam`lamnam')
			


			foreach var of varlist `t'???? {

				local curryear=substr("`var'",`numi',.)
				gen `var'lam`lamnam'_`wav'=.
				replace `var'lam`lamnam'_`wav'=`var'*`var'_wt1`wav'_lam`lamnam'/sumweights`wav'_lam`lamnam' if birthyr<=`curryear' & birthyr>=1941
                replace `var'lam`lamnam'_`wav'=`var'*`var'_wt1`wav'_lam`lamnam'/sumweights`wav'_lam`lamnam' if birthyr<=`curryear' & birthyr<1941
				
			}
		}

		egen `t'provAvg_lam`lamnam'_41mx2=rowtotal(`t'????lam`lamnam'_mx2), missing
		egen `t'provAvg_lam`lamnam'_41mx3=rowtotal(`t'????lam`lamnam'_mx3), missing

		*SD

		foreach wav in `nums' {
			foreach var of varlist `t'???? {
			
				local curryear=substr("`var'",`numi',.)
				gen `var'lam`lamnam'sd_`wav'=.
				replace `var'lam`lamnam'sd_`wav'=`var'_wt1`wav'_lam`lamnam'*(`var'-`t'provAvg_lam`lamnam'_41`wav')^2 if birthyr<=`curryear' & birthyr>=1941
				replace `var'lam`lamnam'sd_`wav'=`var'_wt1`wav'_lam`lamnam'*(`var'-`t'provAvg_lam`lamnam'_41`wav')^2 if birthyr<=`curryear' & birthyr<1941
				
			}
		}
		
		capture drop intSD_lam*

		egen intSD_lam`lamnam'_41mx2=rowtotal(`t'????lam`lamnam'sd_mx2), missing
		egen intSD_lam`lamnam'_41mx3=rowtotal(`t'????lam`lamnam'sd_mx3), missing

		gen `t'provSD_lam`lamnam'_41mx2=sqrt((imp_agemx2)*intSD_lam`lamnam'_41mx2/((imp_agemx2-1)*sumweightsmx2_lam`lamnam'))
		gen `t'provSD_lam`lamnam'_41mx3=sqrt((imp_agemx3)*intSD_lam`lamnam'_41mx3/((imp_agemx3-1)*sumweightsmx3_lam`lamnam'))


		capture drop grbr????lam* intSD* *_wt1*_lam* sumweights*
		
	}
	
	
	capture drop grbr????lam*_?? intSD* *_wt1*_lam* sumweights*
	
	*Variation 2: Attach mexican growth for people who are born before 1941

local nums mx2 mx3
	local lam1=0
	local lam2=0
	local numi=5

	forval lambda1=`lam1'(10)`lam2' {
	
	
	capture drop *_wt1mx2_lam* *_wt1mx3_lam* sumweights* *lam*_mx2 *lam*_mx3 
	
		local lambda=round(`lambda1'/100,0.1)

	

		if `lambda'<0 {
			local hola=substr("`lambda1'",2,.)
			local lamnam neg`hola'
		}
		else {
			local lamnam `lambda1'
		}


		*Avg
		foreach wav in `nums' {
		
		
		forval curryear=1925/2014 {
				
				local k=yearinterview`wav'-`curryear'
				gen v`curryear'_wt1`wav'_lam`lamnam'co2=.
			
				replace v`curryear'_wt1`wav'_lam`lamnam'co2=((imp_age`wav'-`k')/imp_age`wav')^`lambda' if `k'<=imp_age`wav' & `curryear'<=yearinterview`wav' & birthyr>=1925 & birthyr<=`curryear'
				
		
			
				
			}
		


			egen sumweights`wav'_lam`lamnam'co2=rowtotal(*_wt1`wav'_lam`lamnam'co2)
			



				forval curryear=1925/2014 {

				
				gen v`curryear'lam`lamnam'_`wav'co2=.
				
				
				if `curryear'>=1941 {
				
				replace v`curryear'lam`lamnam'_`wav'co2=`t'`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co2/sumweights`wav'_lam`lamnam'co2 if birthyr<=`curryear' & birthyr>=1925
				
				
				}
				
				if `curryear'<1941 {
				
								replace v`curryear'lam`lamnam'_`wav'co2=mex_gr`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co2/sumweights`wav'_lam`lamnam'co2 if birthyr<=`curryear' & birthyr>=1925 & birthyr<=1940
				
				}
				
			}
		}

		egen `t'provAvg_lam`lamnam'_41mx2co2=rowtotal(v????lam`lamnam'_mx2co2), missing
		egen `t'provAvg_lam`lamnam'_41mx3co2=rowtotal(v????lam`lamnam'_mx3co2), missing

		*SD

		foreach wav in `nums' {
		forval curryear=1925/2014 {
				gen v`curryear'lam`lamnam'sd_`wav'co2=.
				
				
						if `curryear'>=1941 {
				
				replace v`curryear'lam`lamnam'sd_`wav'co2=v`curryear'_wt1`wav'_lam`lamnam'co2*(`t'`curryear'-`t'provAvg_lam`lamnam'_41`wav'co2)^2 if birthyr<=`curryear' & birthyr>=1925 


				}
				
					if `curryear'<1941 {
					
				replace v`curryear'lam`lamnam'sd_`wav'co2=v`curryear'_wt1`wav'_lam`lamnam'co2*(mex_gr`curryear'-`t'provAvg_lam`lamnam'_41`wav'co2)^2 if birthyr<=`curryear' & birthyr>=1925  & birthyr<=1940 
							
					}
				
			}
		}
		
		capture drop intSD_lam*

		egen intSD_lam`lamnam'_41mx2co2=rowtotal(v????lam`lamnam'sd_mx2co2), missing
		egen intSD_lam`lamnam'_41mx3co2=rowtotal(v????lam`lamnam'sd_mx3co2), missing

		gen `t'provSD_lam`lamnam'_41mx2co2=sqrt((imp_agemx2)*intSD_lam`lamnam'_41mx2/((imp_agemx2-1)*sumweightsmx2_lam`lamnam'co2))
		gen `t'provSD_lam`lamnam'_41mx3co2=sqrt((imp_agemx3)*intSD_lam`lamnam'_41mx3/((imp_agemx2-1)*sumweightsmx3_lam`lamnam'co2))

		
		capture drop v????lam* intSD* *_wt1*_lam* sumweights*


	}
	
capture drop v????lam* intSD* *_wt1*_lam* sumweights*
	


*Variation 3: Attach Mexican growth for everyone from ages 0-15


local nums mx2 mx3


	local lam1=0
	local lam2=0
	local numi=5

	
	
	forval lambda1=`lam1'(10)`lam2' {
	
	
	capture drop *_wt1mx2_lam* *_wt1mx3_lam* sumweights* *lam*_mx2 *lam*_mx3 
	
		local lambda=round(`lambda1'/100,0.1)



		if `lambda'<0 {
			local hola=substr("`lambda1'",2,.)
			local lamnam neg`hola'
		}
		else {
			local lamnam `lambda1'
		}

*Avg
		
		foreach wav in `nums' {
		
		
		forval curryear=1925/2014 {
				
				local k=yearinterview`wav'-`curryear'
				gen v`curryear'_wt1`wav'_lam`lamnam'co3=.
			
				replace v`curryear'_wt1`wav'_lam`lamnam'co3=((imp_age`wav'-`k')/imp_age`wav')^`lambda' if `k'<=imp_age`wav' & `curryear'<=yearinterview`wav' & birthyr>=1925 & birthyr<=`curryear'
				
		
			
				
			}
		


			egen sumweights`wav'_lam`lamnam'co3=rowtotal(*_wt1`wav'_lam`lamnam'co3)
			



				forval curryear=1925/2014 {

				
				gen v`curryear'lam`lamnam'_`wav'co3=.
				
				
				if `curryear'>=1941 {
				
				replace v`curryear'lam`lamnam'_`wav'co3=`t'`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co3/sumweights`wav'_lam`lamnam'co3 if birthyr<=`curryear' & birthyr>=1925 & `curryear'-birthyr>15 
				
				replace v`curryear'lam`lamnam'_`wav'co3=mex_gr`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co3/sumweights`wav'_lam`lamnam'co3 if birthyr<=`curryear' & birthyr>=1925 & `curryear'-birthyr<=15 
				
				}
				
				if `curryear'<1941 {
				
								replace v`curryear'lam`lamnam'_`wav'co3=mex_gr`curryear'*v`curryear'_wt1`wav'_lam`lamnam'co3/sumweights`wav'_lam`lamnam'co3 if birthyr<=`curryear' & birthyr>=1925 & `curryear'-birthyr<=15 
				
				}
				
			}
		}

		egen `t'provAvg_lam`lamnam'_41mx2co3=rowtotal(v????lam`lamnam'_mx2co3), missing
		egen `t'provAvg_lam`lamnam'_41mx3co3=rowtotal(v????lam`lamnam'_mx3co3), missing

		*SD

		foreach wav in `nums' {
		forval curryear=1925/2014 {
				gen v`curryear'lam`lamnam'sd_`wav'co3=.
				
				
						if `curryear'>=1941 {
				
				replace v`curryear'lam`lamnam'sd_`wav'co3=v`curryear'_wt1`wav'_lam`lamnam'co3*(`t'`curryear'-`t'provAvg_lam`lamnam'_41`wav'co3)^2 if birthyr<=`curryear' & birthyr>=1925  & `curryear'-birthyr>15 

				replace v`curryear'lam`lamnam'sd_`wav'co3=v`curryear'_wt1`wav'_lam`lamnam'co3*(mex_gr`curryear'-`t'provAvg_lam`lamnam'_41`wav'co3)^2 if birthyr<=`curryear' & birthyr>=1925  & `curryear'-birthyr<=15 
				
				}
				
					if `curryear'<1941 {
					
									replace v`curryear'lam`lamnam'sd_`wav'co3=v`curryear'_wt1`wav'_lam`lamnam'co3*(mex_gr`curryear'-`t'provAvg_lam`lamnam'_41`wav'co3)^2 if birthyr<=`curryear' & birthyr>=1925  & `curryear'-birthyr<=15 
					
					}
				
			}
		}
		
		capture drop intSD_lam*

		egen intSD_lam`lamnam'_41mx2co3=rowtotal(v????lam`lamnam'sd_mx2co3), missing
		egen intSD_lam`lamnam'_41mx3co3=rowtotal(v????lam`lamnam'sd_mx3co3), missing

		gen `t'provSD_lam`lamnam'_41mx2co3=sqrt((imp_agemx2)*intSD_lam`lamnam'_41mx2/((imp_agemx2-1)*sumweightsmx2_lam`lamnam'co3))
		gen `t'provSD_lam`lamnam'_41mx3co3=sqrt((imp_agemx3)*intSD_lam`lamnam'_41mx2/((imp_agemx3-1)*sumweightsmx3_lam`lamnam'co3))

		capture drop v????lam* intSD* *_wt1*_lam* sumweights*

	}
	
	capture drop v????lam* intSD* *_wt1*_lam* sumweights*
	

}



keep folio pid_link grbr???? mex_gr???? *provAvg* *provSD* birthstate birthyr yearinterview*

preserve
drop *mx3*
gen mxfls=2
tempfile grs1
save "`grs1'", replace
restore

drop *mx2*
gen mxfls=3
append using "`grs1'"

